<template>
  <div>
    <div class="search">
      <Row>
        <Col>
          <Card>
            <Row>
              <Form ref="searchForm" :model="searchForm" inline :label-width="100" class="search-form">
                <Form-item label="项目名称" prop="">
                  <Input v-model="searchForm.project_name" style="width: 260px;"/>
                </Form-item>
                <Form-item style="margin-left:-100px;" class="br">
                  <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                  <Button @click="handleReset">重置</Button>
                </Form-item>
              </Form>
            </Row>
            <Row class="operation">
              <Button @click="addVehicle" type="primary" icon="md-add">添加派车单</Button>
              <Button @click="editVehicle" type="warning" icon="ios-bookmarks-outline" >编辑派车单</Button>
              <Button @click="viewVehicle" type="success" icon="ios-eye" >查看派车单</Button>
              <Button @click="deleteVehicle" type="error" icon="md-alert" >删除派车单</Button>
              <Button @click="refresh" icon="refresh">刷新</Button>
            </Row>
            <Row>
              <Alert show-icon>
                已选择 <span class="select-count">{{selectCount}}</span> 项
                <a class="select-clear" @click="clearSelectAll">清空</a>
              </Alert>
            </Row>
            <Row>
              <Table
                border
                :loading="loading"
                :columns="columns"
                :data="data"
                sortable="custom"
                @on-selection-change="showSelect"
                ref="table">
              </Table>
              <Table :columns="exportColumns" :data="exportData" ref="exportTable" style="display:none"></Table>
            </Row>
            <Row type="flex" justify="end" class="page">
              <Page
                :current="searchForm.pageNumber"
                :total="total"
                :page-size="searchForm.pageSize"
                @on-change="changePage"
                @on-page-size-change="changePageSize"
                :page-size-opts="[10,20,50]"
                size="small"
                show-total
                show-elevator
                show-sizer
              ></Page>
            </Row>
          </Card>
        </Col>
      </Row>
      <addEdit :data="form" :type="showType" v-model="addEditVisiable" @on-submit="refresh"/>
      <viewVehicle :data="viewForm" v-model="viewVisiable"/>
    </div>
  </div>
</template>
<script>
import { getVehicle, destroyVehicle } from '@/api/work';
import util from '@/libs/util';
import moment from 'moment';
import addEdit from './addEdit';
import viewVehicle from './view';
export default {
  name: 'vehicle-manager',
  components: {
    addEdit, viewVehicle
  },
  data() {
    return {
      loading: false,
      searchForm: {
        project_name: '',
        pageNumber: 1,
        pageSize: 10
      },
      data: [],
      total: 0,
      selectCount: 0,
      exportData: [],
      selectList: [],
      exportColumns: [],
      columns: [
        {
          type: "selection",
          width: 50,
          align: "center",
          fixed: "left"
        },
        {
          type: "index",
          width: 50,
          align: "center",
          fixed: "left"
        },
        {
          title: '派车单编号',
          key: 'vehicle_code',
          width: 100,
          align: 'center'
        },
        {
          title: '项目名称',
          key: 'project_name',
          width: 160,
        },
        {
          title: '出车任务',
          key: 'vehicle_mission',
          width: 220,
        },
        {
          title: '车型/车号',
          key: 'vehicle_number',
          width: 80,
          align: 'center'
        },
        {
          title: '同行人',
          key: 'vehicle_men',
          width: 200,
        },
        {
          title: '开始用车时间',
          key: 'vehicle_start',
          width: 130,
          align: 'center',
          render: (h, params) => {
            let re = moment(params.row.vehicle_start).format('YYYY-MM-DD HH:mm');
            return h("div", re);
          }
        },
        {
          title: '结束用车时间',
          key: 'vehicle_end',
          width: 130,
          align: 'center',
          render: (h, params) => {
            let re = moment(params.row.vehicle_end).format('YYYY-MM-DD HH:mm');
            return h("div", re);
          }
        },
        {
          title: '驾驶员',
          key: 'vehicle_driver',
          width: 80,
          align: 'center'
        },
        {
          title: '申请人',
          key: 'real_name',
          width: 80,
          align: 'center'
        },
        {
            title: "状态",
            key: "state",
            align: "center",
            width: 80,
            render: (h, params) => {
              let re = "";
              if (params.row.state === 1) {
                re = "已审批";
              } else if (params.row.state === 0) {
                re = "未审批";
              }
              return h("div", re);
            }
          },
      ],
      addEditVisiable: false,
      viewVisiable: false,
      form: {},
      showType: '0',
      viewForm: {}
    }
  },
  methods: {
    getVehicleList() {
      this.loading = true;
      getVehicle(this.searchForm).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.data = res.data.rows;
          this.total = res.data.count;
        }
      })
    },
    init() {
      this.getVehicleList();
    },
    handleSearch() {
      this.searchForm.project_name = this.searchForm.project_name.trim();
      if (this.searchForm.project_name.length > 0) {
        this.searchForm.pageNumber = 1;
        this.getVehicleList();
      } else {
        this.$Message.warning('请输入要搜索的项目名称！');
        return;
      }
    },
    handleReset() {
      this.searchForm.pageNumber = 1;
      this.searchForm.pageSize = 10;
      this.getVehicleList();
    },
    addVehicle() {
      const hasPermission = util.oneOf('add', this.$route.meta.permTypes);
      if (!hasPermission) {
        this.$Message.error('抱歉，您没有操作权限！');
        return;
      }; 
      this.showType = '1';
      this.form = {vehicle_oil: 0};
      this.addEditVisiable = true;
    },
    editVehicle() {
      const hasPermission = util.oneOf('edit', this.$route.meta.permTypes);
      if (!hasPermission) {
        this.$Message.error('抱歉，您没有操作权限！');
        return;
      }; 
      if (this.selectList.length !== 1) {
        this.$Message.warning('必须选择一条记录进行操作!');
        return;
      }
      this.showType = '2';
      this.form = this.selectList[0];
      this.form.apply_date = [moment(this.selectList[0].vehicle_start).format('YYYY-MM-DD HH:mm:ss'), moment(this.selectList[0].vehicle_end).format('YYYY-MM-DD HH:mm:ss')];
      this.addEditVisiable = true;
    },
    viewVehicle() {
      const hasPermission = util.oneOf('view', this.$route.meta.permTypes);
      if (!hasPermission) {
        this.$Message.error('抱歉，您没有操作权限！');
        return;
      }; 
      if (this.selectList.length !== 1) {
        this.$Message.warning('必须选择一条记录进行操作!');
        return;
      }
      this.viewForm = this.selectList[0];
      this.viewForm.begin_time = moment(this.selectList[0].vehicle_start).format('YYYY-MM-DD HH:mm');
      this.viewForm.end_time = moment(this.selectList[0].vehicle_end).format('YYYY-MM-DD HH:mm');
      this.viewVisiable = true;
    },
    deleteVehicle() {
      const hasPermission = util.oneOf('delete', this.$route.meta.permTypes);
      if (!hasPermission) {
        this.$Message.error('抱歉，您没有操作权限！');
        return;
      }; 
      if (this.selectList.length !== 1) {
        this.$Message.warning('必须选择一条记录进行操作!');
        return;
      }
      if (this.selectList[0].state === 1) {
        this.$Message.warning('此派车单已经审批过了，不允许删除!');
        return;
      }
      this.$Modal.confirm({
        title: '确认删除',
        content: '删除不可恢复，你确认要删除'+this.selectList[0].project_name+'工程的这张派车单吗？',
        onOk: () => {
          destroyVehicle({id: this.selectList[0].id}).then(res => {
            if (res.code === 0) {
              this.$Message.success(res.message);
              this.getVehicleList();
            }
          })
        }
      });
    },
    refresh() {
      this.getVehicleList();
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    showSelect(e) {
      this.exportData = e;
      this.selectList = e;
      this.selectCount = e.length;
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getVehicleList();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getVehicleList();
    },
  },
  mounted() {
    this.init();
  }
}
</script>
<style lang="less" scoped>
@import "./vehicleManager.less";
</style>